package com.fzy.common.entity;

import com.fzy.common.enums.ErrorCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 冯赵杨
 * @date 2018/7/18 上午10:19
 */
@Data
public class Result {

    private boolean success = true;

    private String status;

    private Integer code;

    private String message;

    private Object data;


    public static Result build(String status, Integer code, String message, Object data) {
        return new Result(status, code, message, data);
    }

    public static Result ok(Object data) {
        return new Result(data);
    }

    /**
     * 飞冰在接收参数的时候自动去找json中的data字段，不用加data,
     * 前端单选框或复选框dataSource调用示例：this.props.bindingData.listUser.list, listUser是前端自定义的变量
     *
     * @param data 数组或者List集合
     * @return
     * @author 冯赵杨
     * @create 2018/7/26
     */
    public static Result okList(Object data) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("list", data);
        return new Result(dataMap);
    }

    public static Result ok() {
        return new Result(null);
    }

    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setStatus("fail");
        result.setCode(code);
        result.setMessage(msg);
        result.setSuccess(false);
        return result;
    }

    public static Result error(ErrorCode errorCode) {
        Result result = new Result();
        result.setStatus("fail");
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMsg());
        result.setSuccess(false);
        return result;
    }

    public Result() {

    }

    public static Result build(String status, Integer code, String message) {
        return new Result(status, code, message, null);
    }

    public Result(String status, Integer code, String message, Object data) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public Result(Object data) {
        this.code = 200;
        this.status = "success";
        this.message = "";
        this.data = data;
    }

    public static Result fail() {
        return new Result("fail", 500, "服务器接口异常", null);
    }

    public static Result fail(String message) {
        return fail(message, null);
    }

    /**
     * 默认返回参数不合法的CODE
     *
     * @param message
     * @param data
     * @return
     * @author 冯赵杨
     * @create 2018/8/7
     */
    public static Result fail(String message, Object data) {
        int code = ErrorCode.PARAM_NOT_LEGAL.getCode();
        String msg = ErrorCode.PARAM_NOT_LEGAL.getMsg();
        if (null != message || !"".equals(message)) {
            msg = message;
        }

        return new Result("fail", code, msg, data);
    }


    /**
     * 返回键值对数据
     *
     * @param key
     * @param value
     * @return
     * @author 冯赵杨
     * @create 2018/8/2
     */
    public static Result failKV(String key, Object value) {
        Map<String, Object> dataMap = new HashMap<>(2);
        dataMap.put("key", key);
        dataMap.put("value", value);
        return fail("", dataMap);
    }

}
